package com.sh.caryr.erp.excel;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.util.PoiPublicUtil;
import com.sh.caryr.erp.entity.product.Attribute;
import com.sh.caryr.erp.entity.product.Product;
import com.sh.caryr.erp.entity.trade.Order;
import com.sh.caryr.erp.entity.trade.TradeOrder;
import org.junit.Test;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ImportFromExcel {

    public static List<Product> importNormalProducts(File file) {
        ImportParams params = new ImportParams();
        params.setHeadRows(2);
        return ExcelImportUtil.importExcel(file, Product.class, params);
    }

    public static List<Attribute> importAttributes(File file) {
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        return ExcelImportUtil.importExcel(file, Attribute.class, params);
    }

    public static List<Order> importTrade(File file, boolean hasUpdateAuth) {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(2);
        List<Order> orderList = ExcelImportUtil.importExcel(file, Order.class, params);
        return filterInvalidTrades(orderList, hasUpdateAuth);
    }

    public static List<Order> importTrade(InputStream inputStream) throws Exception {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(2);

        List<Order> list = new ArrayList<>();
        return ExcelImportUtil.importExcel(inputStream, Order.class, params);
    }

    public static List<Order> filterInvalidTrades(List<Order> orders, boolean hasUpdateAuth) {
        List<Order> newTrades = new ArrayList<>();
        if (orders == null || orders.isEmpty()) {
            return newTrades;
        }

        for (Order trade : orders) {
            if (trade.getId() != null && !hasUpdateAuth) { // Update trade but has no authorization
                continue;
            }
            if (trade.getReceiver() == null) {  //Have no trade receiver
                continue;
            }
            List<TradeOrder> newOrders = new ArrayList<>();
            for (TradeOrder order : trade.getTradeOrderList()) {
                if (order.getProductSpecId() != null &&
                        order.getProductSpecId() != 0 &&
                        order.getProductSpecAmount() != null &&
                        order.getProductSpecAmount() != 0) {
                    newOrders.add(order);
                }
            }
            if (!newOrders.isEmpty()) {
                trade.setTradeOrderList(newOrders);
                newTrades.add(trade);
            }
        }
        return newTrades;
    }

    @Test
    public void test() {
        List<Order> list = ImportFromExcel.importTrade(new File(PoiPublicUtil.getWebRootPath("../resources/import/trade_order.xlsx")), true);
        System.out.println(list.size());
        System.out.println(list);
        System.out.println(list.get(0).getTradeOrderList().size());
        System.out.println(list.get(0).getTradeOrderList());
        System.out.println(list.get(1).getTradeOrderList().size());
        System.out.println(list.get(1).getTradeOrderList());

    }
}
